package com.dyh.quickdemozwei;

import com.google.common.collect.Lists;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Author: yhdong
 * @Date: 2021/9/30 15:03
 */
@Slf4j
public class ThreadPoolHelper {
    ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(2,2,
                    10, TimeUnit.SECONDS,
                    new LinkedBlockingQueue<>(100),
                    new ThreadFactoryBuilder().setNameFormat("TaobaoItemInfoTranser-%d").build(),
                    new ThreadPoolExecutor.CallerRunsPolicy());

    static Map<String,Integer> code2Limit = new HashMap<>();
    static {
        code2Limit.put("A",2);
        code2Limit.put("B",1);
        code2Limit.put("C",1);
    }



    @Test
    public void test() throws InterruptedException {
        List<String> tasks = Lists.newArrayList("A","B","A","A","C","A","B","C","C");
        passTasks(tasks);

    }

    public void passTasks(  List<String> tasks) throws InterruptedException {
        log.info("task={}",tasks);
        Map<String,AtomicInteger> code2count = new HashMap<>();
        code2count.put("A",new AtomicInteger(0));
        code2count.put("B",new AtomicInteger(0));
        code2count.put("C",new AtomicInteger(0));

        List<String> next = new ArrayList<>();
        for (String task : tasks) {
            AtomicInteger atomicInteger = code2count.get(task);
            if (atomicInteger.get()>code2Limit.get(task)){
                next.add(task);
            }
            else {
                atomicInteger.incrementAndGet();
                runTask(task);
            }
        }
        if (next.size()>0)
            passTasks(next);
    }

    private void runTask(String task) throws InterruptedException {
        Thread.sleep(500);
        log.info("{} {}",Thread.currentThread().getName(),task);
    }

    @Test
    public void test1() throws InterruptedException {
        /*
        15:44:39.452 [Thread-2] INFO com.dyh.quickdemozwei.ThreadPoolHelper - Thread-2 0
15:44:39.452 [Thread-3] INFO com.dyh.quickdemozwei.ThreadPoolHelper - Thread-3 0
15:44:39.452 [Thread-1] INFO com.dyh.quickdemozwei.ThreadPoolHelper - Thread-1 0
3
        */
        Map<String, AtomicInteger> countMap = new HashMap<>();
        countMap.put("A",new AtomicInteger(0));

        Runnable r= ()->{
            log.info("{} {}",Thread.currentThread().getName(),countMap.get("A"));
            countMap.get("A").incrementAndGet();
        };



        new Thread(r).start();
        new Thread(r).start();
        new Thread(r).start();

        Thread.sleep(100L);
        System.out.println(countMap.get("A"));
    }


    @Test
    public void test2() throws InterruptedException {
        Map<String, Integer> countMap = new HashMap<>();
        countMap.put("A",0);

        Runnable r= ()->{
            log.info("{}",Thread.currentThread().getName());
            Integer count =  countMap.get("A");
            log.info("{} {}",Thread.currentThread().getName(),count);
            countMap.put("A",count+1);
        };



      new Thread(r).start();
      new Thread(r).start();
      new Thread(r).start();

        Thread.sleep(100L);
        System.out.println(countMap.get("A"));
    }


    @Test
    public void test3() throws InterruptedException {
        ConcurrentHashMap<String,Integer> countMap=new ConcurrentHashMap<>();
        countMap.put("A",0);

        Runnable r= ()->{
            log.info("{}",Thread.currentThread().getName());
            Integer count =  countMap.get("A");
            log.info("{} {}",Thread.currentThread().getName(),count);
            countMap.put("A",count+1);
        };




        new Thread(r).start();
        new Thread(r).start();
        new Thread(r).start();

        Thread.sleep(100L);
        System.out.println(countMap.get("A"));
    }

    class MyThread extends Thread{
        @Override
        public void run() {
            super.run();
        }
    }
}
